﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace POSPlus.EZBarTender
{
    public partial class BatchSummary : Form
    {
        private DataSet _set;
        private DataTable _table;
        private BackgroundableDataGridView _view;

        public BatchSummary()
        {
            InitializeComponent();
            _set = new DataSet();
        }

        public BatchSummary(DataTable dataTable, string title)
        {
            InitializeComponent();
            Text = title;
            _view = new BackgroundableDataGridView();
            _view.Size = new System.Drawing.Size(564, 285);
            _tabControl.SelectedTab.Controls.Add(_view);

            _set = new DataSet();
            _table = dataTable;
            _set.Tables.Add("Printable");
            _set.Tables.Add("Non Printable");

            foreach (DataColumn col in _table.Columns)
            {
                _set.Tables["Printable"].Columns.Add(col.ColumnName);
                _set.Tables["Non Printable"].Columns.Add(col.ColumnName);
            }
        }

        public static DialogResult ShowDialog(DataTable dataTable, string title)
        {
            using (BatchSummary dialog = new BatchSummary(dataTable, title))
            {
                DialogResult result = dialog.ShowDialog();
                return result;
            }
        }

        private void BatchSummary_Load(object sender, EventArgs e)
        {
            backgroundWorker.RunWorkerAsync();
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (DataRow row in _table.Rows)
            {
                if (row["SVTregPrintTagFlag"].ToString().Equals("Y"))
                    _set.Tables["Printable"].ImportRow(row);
                else
                    _set.Tables["Non Printable"].ImportRow(row);
            }
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _view.DataSource = _set.Tables["Printable"];
        }

        private void _tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            _view.DataSource = _set.Tables[_tabControl.SelectedTab.Text];
            _tabControl.SelectedTab.Controls.Remove(_view);
            _tabControl.SelectedTab.Controls.Add(_view);
            if (_tabControl.SelectedTab.Text.Equals("Non Printable"))
                _btnPrint.Enabled = false;
            else
                _btnPrint.Enabled = true;
        }
    }
}
